package chalmers.game.subterranean.event;

import com.google.common.eventbus.EventBus;

/**
 * This enum only has the value INSTANCE wich holds an google guava EventBus.
 * 
 * @author Alex Evert
 */
public enum Bus {

  /**
   * The element that holds the bus.
   */
  INSTANCE;

  /**
   * The shared instance of an EventBus.
   */
  private final EventBus eventBus;

  /**
   * Private constructor that creates the shared instance.
   */
  private Bus() {
    eventBus = new EventBus();
  }

  /**
   * Register an object to subscribe to the eventbus. To set a method as the eventlistener annotate
   * it with @Subscribe and make it take exactly one argument of the event type and nothing else.
   * 
   * @param subscriber The object that wants to subscribe.
   */
  public void register(Object subscriber) {
    eventBus.register(subscriber);
  }

  /**
   * Post an object to anyone listening to that type, listeners of more general types will also get
   * the event. Listen for the deadEvent if you want to see if noone got the event.
   * 
   * @param event The event that shall be posted
   */
  public void post(Object event) {
    eventBus.post(event);
  }

  public void unRegister(Object unsubscriber) {
    eventBus.unregister(unsubscriber);
  }
}
